package com.petalsgroup.petals.web.servlet.user;


import com.petalsgroup.petals.pojo.*;
import com.petalsgroup.petals.service.user.CommentsService;
import com.petalsgroup.petals.service.user.MySelfService;
import com.petalsgroup.petals.service.user.impl.CommentsServiceImpl;
import com.petalsgroup.petals.service.user.impl.MySelfServiceImpl;
import com.petalsgroup.petals.util.MailUtils;
import com.petalsgroup.petals.util.Md5Util;
import com.petalsgroup.petals.web.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@WebServlet("/myself/*")
public class MySelfServlet extends BaseServlet {
    private MySelfService mySelfService = new MySelfServiceImpl();
    private CommentsService commentsService = new CommentsServiceImpl();

    //跳转到个人信息
    public void toMySelf(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.getSession().removeAttribute("search");
        //去获取粉丝数

        req.getRequestDispatcher("/WEB-INF/user_view/myself.jsp").forward(req,resp);
    }
    //评论信息加载
    public void commentsList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取评论信息
        //从session中获取uid
        UserDO user =(UserDO) req.getSession().getAttribute("user");
        Integer uid = user.getUid();
        //System.out.println(uid);
        //通过uid查询评论信息 返回list
        List<CommentsVO> list =  commentsService.findCommentsByUid(uid);
        //System.out.println(list.toString());
        //将list存入session
        req.getSession().setAttribute("commentsList",list);
        Integer attentionNum = mySelfService.findAttentionNum(uid);
        req.getSession().setAttribute("myAttentionNum",attentionNum);
        resp.sendRedirect(req.getContextPath()+"/myself/collectList");
    }
    //收藏信息加载
    public void collectList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取评论信息
        //从session中获取uid
        UserDO user =(UserDO) req.getSession().getAttribute("user");
        Integer uid = user.getUid();
        //System.out.println(uid);
        //通过uid查询收藏信息 返回list
        List<AnimationSimpleVO> list = mySelfService.findAidByUid(uid);
        //System.out.println(list.toString());
        //将list存入session
        req.getSession().setAttribute("collectList",list);


        resp.sendRedirect(req.getContextPath()+"/myself/toMySelf");
    }


    //跳转到修改信息
    public void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/user_view/update.jsp").forward(req,resp);
    }

    //修改用户信息
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserDO user =(UserDO) req.getSession().getAttribute("user");
        //System.out.println(user.toString());
        String name = req.getParameter("name");
        String pass = req.getParameter("password");
        String email = req.getParameter("email");
        user.setName(name);
        //加密
        String password = Md5Util.convertMD5(pass);
        user.setPassword(password);
        user.setEmail(email);

        //System.out.println(user);

        //更新方法
        Boolean b = mySelfService.updateUser(user);
        //System.out.println(b);
        ResultInfo resultInfo = new ResultInfo();
        if (b){
            resultInfo.setFlag(true);
        }else {
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("更新失败");
        }
        //响应回前端
        writeValue(resp,resultInfo);
        //更新session中的user
        req.getSession().setAttribute("user",user);
    }

    //删除评论
    public void deleteComments(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<CommentsVO> list =(List<CommentsVO>) req.getSession().getAttribute("commentsList");
        //System.out.println(list);

        Integer comid = list.get(0).getCommentsDO().getCom_id();
        //Integer aid = list.get(0).getCommentsDO().getAid();
        //根据评论id删除
        commentsService.deleteCommentsByid(comid);
        resp.sendRedirect(req.getContextPath()+"/myself/commentsList");
    }

    //删除选中
    public void deleteAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //List<CommentsVO> list =(List<CommentsVO>) req.getSession().getAttribute("commentsList");
        String[] cids = req.getParameterValues("selected");
        //System.out.println(Arrays.toString(cids));
        for (int i = 0; i < cids.length; i++) {
            Integer cid = Integer.parseInt(cids[i]);
            //System.out.println(cid);
            commentsService.deleteCommentsByid(cid);
        }

        resp.sendRedirect(req.getContextPath()+"/myself/commentsList");
    }

    //取消选中
    public void cancelAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String[] aids = req.getParameterValues("collectselected");
        UserDO user =(UserDO) req.getSession().getAttribute("user");
        Integer uid = user.getUid();
        for (int i = 0; i < aids.length; i++) {
            Integer aid = Integer.parseInt(aids[i]);
            mySelfService.cancelCollect(aid,uid);
        }
        resp.sendRedirect(req.getContextPath()+"/myself/commentsList");
    }

    //查询动漫
    public void selectAnimation(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String selectname = req.getParameter("selectname");
        //System.out.println(selectname);
        if (selectname.equals("查询所有")){
            resp.sendRedirect(req.getContextPath()+"/myself/commentsList");
        }else {
             //通过动漫名 查询
            List<CommentsVO> list =  commentsService.findCommentsByAname(selectname);
            //System.out.println(list.toString());
            //将list存入session
            req.getSession().setAttribute("commentsList",list);
            resp.sendRedirect(req.getContextPath()+"/myself/toMySelf");
        }
    }

    //查询收藏
    public void selectCollect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("selectCollectname");
        //System.out.println(name);
        if(name.equals("查询所有")){
            resp.sendRedirect(req.getContextPath()+"/myself/collectList");

        }else {
            List<AnimationSimpleVO> list =mySelfService.findCollectByAname(name);
            //System.out.println(list);
            req.getSession().setAttribute("collectList",list);
            resp.sendRedirect(req.getContextPath()+"/myself/toMySelf");
        }

    }

    //取消收藏
    public void cancelCollect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //aid
        //String id = req.getParameter("collectselected");
        List<AnimationSimpleVO> list =(List<AnimationSimpleVO>) req.getSession().getAttribute("collectList");
        System.out.println(list);
        Integer aid = list.get(0).getAnimationSimple().getAid();
        System.out.println(aid);
        //uid
        UserDO user =(UserDO) req.getSession().getAttribute("user");
        Integer uid = user.getUid();
        //调用 取消收藏的方法
        Boolean b = mySelfService.cancelCollect(aid,uid);
        if (b){
            //重新加载
            resp.sendRedirect(req.getContextPath()+"/myself/collectList");
        }
    }
}
